<template>
	<view class="shopcart">
		<!-- <view class="content" @click="toggleList">
			<view class="content-left">
				<view class="logo-wrapper">
					<view class="logo" :class="{'highlight':totalCount>0}">
						<uv-icon name="shopping-cart-fill" size="36" :color="totalCount>0?'#ffffff':'#999999'" />
					</view>
					<view class="num" v-show="totalCount>0">
						{{totalCount}}
					</view>
				</view>
				<view class="price" :class="{'highlight':totalPrice>0}">￥{{totalPrice}}</view>
			</view>
			<view class="content-right" @click="pay">
				<view class="pay" :class="payClass">
					{{payDesc}}
				</view>
			</view>
		</view> -->
		<view class="ball-container">
			<view v-for="(ball,index) in balls" :key="index">
				<transition @before-enter="beforeDrop" @enter="dropping" @after-enter="afterDrop">
					<view class="ball" v-show="ball.show">
						<view class="inner inner-hook"></view>
					</view>
				</transition>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapActions
	} from "vuex";
	const BALL_LEN = 10;
	const innerClsHook = 'inner-hook';

	function createBalls() {
		let balls = [];
		for (let i = 0; i < BALL_LEN; i++) {
			balls.push({
				show: false
			});
		}
		return balls;
	}

	export default {
		name: 'ShopCart',
		props: {
			selectFoods: {
				type: Array,
				default: () => [],
			},
			fold: {
				type: Boolean,
				default: true,
			},
			shopCartList: {
				type: Array,
				default: () => [],
			},
		},
		data() {
			return {
				balls: createBalls(),
			};
		},
		watch: {
			fold(newVal) {
				this.listFold = newVal;
			},
			totalCount(count) {
				if (!this.fold && count === 0) {
					this.hideShopCartList();
				}
			},
		},
		created() {
			this.dropBalls = [];
		},
		computed: {
			...mapState("shopCart", ["listFold"]),
			totalPrice() {
				return this.selectFoods.reduce((total, food) => total + food.price * food.count, 0);
			},
			totalCount() {
				return this.selectFoods.reduce((count, food) => count + food.count, 0);
			},
			payDesc() {
				return this.totalPrice === 0 ? '预定' : '预定';
			},
			payClass() {
				return this.totalCount ? 'enough' : 'not-enough';
			}
		},
		methods: {
			...mapActions("shopCart", ["toggleListFold"]),
			toggleList() {
				if (!this.totalCount) return;
				this.toggleListFold();
			},
			dropItem(el) {
				this.$refs.shopCartSticky.drop(el);
			},
			hideShopCartList() {
				this.listFold = true;
			},
			hideShopCartSticky() {
				this.$refs.shopCartSticky.hide();
			},
			foldList() {
				this.listFold = true;
			},
			drop(el) {
				for (const ball of this.balls) {
					if (!ball.show) {
						ball.show = true;
						ball.el = el;
						this.dropBalls.push(ball);
						break;
					}
				}
			},
			beforeDrop(el) {
				const ball = this.dropBalls[this.dropBalls.length - 1];
				const rectX = ball.el.x;
				const rectY = ball.el.y;
				const x = rectX - 40;
				const y = -(window.innerHeight - rectY - 22);
				el.style.display = '';
				el.style.transform = el.style.webkitTransform = `translate3d(0,${y}px,0)`;
				const inner = el.getElementsByClassName(innerClsHook)[0];
				inner.style.transform = inner.style.webkitTransform = `translate3d(${x}px,0,0)`;
			},
			dropping(el, done) {
				this._reflow = document.body.offsetHeight;

				// 设置最终的 transform
				el.style.transform = el.style.webkitTransform = `translate3d(${(window.innerWidth - 80) / 2}px, 0, 0)`;
				const inner = el.getElementsByClassName(innerClsHook)[0];
				inner.style.transform = inner.style.webkitTransform = `translate3d(0, 0, 0)`;

				// 动画结束后调用 done
				el.addEventListener('transitionend', done);
			},
			afterDrop(el) {
				const ball = this.dropBalls.shift();
				if (ball) {
					ball.show = false;
					el.style.display = 'none';
				}
			}
		}
	};
</script>

<style scoped lang="scss">
	.shopcart {
		height: 100%;
	}

	.content {


		display: flex;
		background: #000000;
		font-size: 0;
		color: #999;
	}

	.content-left {
		flex: 1;
	}

	.logo-wrapper {
		display: inline-block;
		vertical-align: top;
		position: relative;
		top: -10px;
		margin: 0 12px;
		padding: 6px;
		width: 56px;
		height: 56px;
		box-sizing: border-box;
		border-radius: 50%;
		background: #000000;
	}

	.logo {
		width: 100%;
		height: 100%;
		border-radius: 50%;
		text-align: center;
		background: #333;
		display: flex;
		align-items: center;
		justify-content: center;

		&.highlight {
			background: #333;
		}
	}

	.icon-shopping_cart {
		line-height: 44px;
		font-size: 14px;
		color: #333;


		&.highlight {
			color: #333;
		}
	}

	.num {
		position: absolute;
		top: -6rpx;
		right: -6rpx;
		font-size: 24rpx;
		background-color: #ff6506;
		color: #ffffff;
		border-radius: 50%;
		width: 36rpx;
		height: 36rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		border: 1rpx solid #ffffff;
	}

	.price {
		display: inline-block;
		vertical-align: top;
		margin-top: 12px;
		line-height: 24px;
		padding-right: 12px;
		box-sizing: border-box;
		border-right: 1px solid rgba(255, 255, 255, 0.1);
		font-weight: 700;
		font-size: 32rpx;

		&.highlight {
			color: #fff
		}
	}

	.desc {
		display: inline-block;
		vertical-align: top;
		margin: 12px 0 0 12px;
		line-height: 24px;
		font-size: 12px;
	}

	.content-right {
		flex: 0 0 105px;
		width: 105px;
	}

	.pay {
		height: 48px;
		line-height: 48px;
		text-align: center;
		font-weight: 700;
		font-size: 12px;


		&.not-enough {
			background: #333;
		}

		&.enough {
			background: #333;
			color: #fff;
		}
	}

	.ball-container {
		.ball {
			position: fixed;
			left: 32px;
			bottom: 22px;
			z-index: 200;
			transition: all 0.4s cubic-bezier(0.3, -0.25, 0.84, 0.64);
		}

		.inner {
			width: 16px;
			height: 16px;
			border-radius: 50%;
			background: #333;
			transition: all 0.4s linear;
		}
	}
</style>